package com.orange.common.autoconfig.template;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @Description:对百度云人脸识别的sdk进行封装
 * 该类已经被spring管理
 */
@Component
@ConditionalOnProperty(prefix = "orange.autoconfig.baidu.aip.face" , name = "enable",havingValue = "true",matchIfMissing = false)
public class AipFaceTemplate {

    @Resource
    AipFace client;

    /**
     * 检测图片中是否包含人脸
     *  true：包含
     *  false：不包含
     */
    public boolean detect(String imageUrl) {
        // 调用接口
        String imageType = "URL";

        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", "age");
        options.put("max_face_num", "2");
        options.put("face_type", "LIVE");
        options.put("liveness_control", "LOW");

        // 人脸检测
        JSONObject res = client.detect(imageUrl, imageType, options);
        System.out.println(res.toString(2));

        Integer error_code = (Integer) res.get("error_code");
        boolean flag=false;
        if (error_code==0){
           flag=true;
        }
        return flag;
    }
}
